﻿{% extends 'admin/base.html' %}

{%load comm_extras %}

{% block head_soruce %}
<script type="text/javascript" src="/static/common/js/jquery.simplemodal.1.4.min.js"></script> 
<script type="text/javascript" src="/static/admin/js/admin_master.js"></script> 
<script type="text/javascript">
	function checkData(){
		var re = /^[1-9]+[0-9]*]*$/;
		var tuan_salecount = jQuery.trim($("#tuan_salecount").val());
		if(tuan_salecount == "" || !re.test(tuan_salecount)){
			alert("请输入正确的团购数量");
			return false;
		}
		var tuan_url = jQuery.trim($("#tuan_url").val());
		if(tuan_url == ""){
			alert("团购网址不能为空");
			return false;
		}
		var tuan_saledate = jQuery.trim($("#tuan_saledate").val());
		re = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
		if(tuan_saledate == "" || !re.test(tuan_saledate)){
			alert("请输入正确的团购时间");
			return false;
		}
		var supplier_id = jQuery.trim($("#selSupplyId").val());
		var tuan_price = jQuery.trim($("#tuan_price").val());
		if(tuan_price == "" || isNaN(tuan_price)){
			alert("请输入团购价");
			return false;
		}
		var tuan_sitename = jQuery.trim($("#tuan_sitename").val());
		if(tuan_sitename == ""){
			alert("团购网站名称不能为空");
			return false;
		}
		var tuan_context = jQuery.trim($("#tuan_context").val());
		if(tuan_context == ""){
			alert("团购说明不能为空");
			return false;
		}
		
		var url = "{%url admin-tuangou-add-save-json tuangou_id %}";

        var	data = {'tuan_salecount':tuan_salecount, 'tuan_url':tuan_url, 'supplier_id':supplier_id, 'tuan_price':tuan_price, 'tuan_sitename':tuan_sitename, 'item_id':{{product_id}}, 'tuan_saledate':tuan_saledate, 'tuan_context':tuan_context};
		$.ajax({
			url: url,
			type: 'post',
			dataType: 'json',
			data:data,
			success: function(result, textStatus){
				if(result){
					if(result.success){
						window.location.href = "{%url admin-tuangou-index product_id %}";
					}else{
						alert(result.err_msg);
					}
				}else{
					alert('保存失败');
				}
			},
			error: function(xhr, textStatus, errorThrown){
			}
		});

		return false;
	}

</script>
{% endblock %}

{% block main_content %}

<form id="form1" name="form1" method="POST">
	<ul id="movie-form" class="form">
	    <li>
            <label for="title">商品名称:</label>
			<img src="/static/productimgs/{{item.homeimage.thumburl}}" width="50" height="50" alt="{{item.title}}" />
            {{item.title}}
        </li>
		<li>
            <label for="price">团购数量:</label>
            <input type="text" id="tuan_salecount" name="tuan_salecount" value="{{tuangou.tuan_salecount}}" class="inputText" />
        </li>
		<li>
			<label for="tuan_url">团购网址:</label>
            <input type="text" id="tuan_url" name="tuan_url" value="{{tuangou.tuan_url}}" class="inputText" />
		</li>
		<li>
			<label for="selSupplyId">供应商:</label>
            <select id="selSupplyId">
				<option value ="0">无</option>
				{%for s in supplys%}
					<option value ="{{s.id}}" {%if s.selected%} selected="true" {%endif%}>{{s.title}}</option>
				{%endfor%}
			</select>
		</li>
		<li>
			<label for="tuan_saledate">团购时间:</label>
            <input type="text" id="tuan_saledate" name="tuan_saledate" value="{{tuangou.tuan_saledate}}" class="inputText" />
		</li>
		<li>
            <label for="tuanprice">团购价:</label>
            <input type="text" id="tuan_price" name="tuan_price" value="{{tuangou.tuan_price}}" class="inputText" />
        </li>
		<li>
			<label for="tuan_sitename">团购网站名称:</label>
            <input type="text" id="tuan_sitename" name="tuan_sitename" value="{{tuangou.tuan_sitename}}" class="inputText" />
		</li>
		<li>
			<label for="tuan_sitename">团购说明:</label>
            <textarea id="tuan_context" name="tuan_context" class="inputText" >{{tuangou.tuan_context}}</textarea>
		</li>
		<li>
			<br/>
			<input type="submit" id="btnAdd" value="保存" onclick="return checkData();" style="margin-left:500px"/>
		</li>
	</ul>

</form>
{% endblock %}
